#!/bin/sh

#set DEBUG=1 for write breakpoint info to logfile
DEBUG=0
#Logfile. It rewrite every run
LOG=/conf/.cron/runparts.log
#Date for breakpoints
DATE=`date +"%F %T"`

BINPATH="/conf/sbin"
CONFPATH="/conf/.config"

if [ -s $CONFPATH/tweakpack.conf ]; then
    . $CONFPATH/tweakpack.conf
fi

[ $DEBUG -ge 1 ] && echo "$DATE: = START runparts.sh =" > $LOG 

#exit 0
if [ -f /conf/.cron/isrun.lock ]; then
    [ $DEBUG -ge 1 ] && echo "$DATE: Found /conf/.cron/isrun.lock. runparts already started or /conf/.cron/isrun.lock stalled. Exiting" >> $LOG 
    exit 0
fi
[ $DEBUG -ge 1 ] && echo "$DATE: Create lock file" >> $LOG
touch /conf/.cron/isrun.lock

if [ `netstat -lean | grep -c 9091` -le 0 ]; then
    [ $DEBUG -ge 1 ] && echo "$DATE: Transmission not started. Nothing liste port 9091" >> $LOG
    if [ -d /mnt/data/public ]; then
        [ $DEBUG -ge 1 ] && echo "$DATE: Found mounted HDD at /mnt/data. Try to run transmission" >> $LOG
        if [ -x $BINPATH/transmission-daemon ]; then
	    export TRANSMISSION_WEB_HOME=/conf/transmission/web
            nice -n 10 $BINPATH/transmission-daemon -g $CONFPATH
            RETVAL=$?
        else
            [ $DEBUG -ge 1 ] && echo "$DATE: $BINPATH/transmission-daemon not found" >> $LOG
            RETVAL=1
        fi
        [ $DEBUG -ge 1 ] && echo "$DATE: Return value: $RETVAL" >> $LOG
        if [ $RETVAL -ne 0 ]; then
            [ $DEBUG -ge 1 ] && echo "$DATE: Transmission not started. Exiting" >> $LOG
            exit 0
        fi
        [ $DEBUG -ge 1 ] && echo "$DATE: Create lock file for first run" >> $LOG
        touch /conf/.cron/firstrun.lock
    else
        [ $DEBUG -ge 1 ] && echo "$DATE: Not found mounted HDD. Transmission not started" >> $LOG
        exit 0
    fi
else
    [ $DEBUG -ge 1 ] && echo "$DATE: Transmission already started or some else listen port 9091" >> $LOG
fi

[ $DEBUG -ge 1 ] && echo "$DATE: == QUEUE PROCESSING ==" >> $LOG

if [ `netstat -lean | grep -c 9091` -ge 1 ]; then
    [ $DEBUG -ge 1 ] && echo "$DATE: Transmission already started or some else listen port 9091" >> $LOG
    if [ ! -f /conf/.cron/firstrun.lock ]; then
        [ $DEBUG -ge 1 ] && echo "$DATE: This is not first run. Lock file not found" >> $LOG
        [ $DEBUG -ge 1 ] && echo "$DATE: Try to run queue.sh" >> $LOG
        nice -n 10 /conf/.cron/queue.sh
        RETVAL=$?
        [ $DEBUG -ge 1 ] && echo "$DATE: Return value: $RETVAL" >> $LOG
    else
        [ $DEBUG -ge 1 ] && echo "$DATE: This is first run. Found lock file" >> $LOG
    fi
else
    [ $DEBUG -ge 1 ] && echo "$DATE: Transmission not started" >> $LOG
fi

[ $DEBUG -ge 1 ] && echo "$DATE: == NTP PROCESSING ==" >> $LOG
NTP=`cat /etc/crontab | grep ntpdate | grep -v grep | wc -l`
[ $NTP -lt 1 ] && echo "0 1 * * * root /usr/sbin/ntpdate  ntp.belbone.be >/dev/null 2>&1 &" >> /etc/crontab

[ $DEBUG -ge 1 ] && echo "$DATE: == REMOVE LOCK FILES ==" >> $LOG

rm -f /conf/.cron/isrun.lock
rm -f /conf/.cron/firstrun.lock

[ $DEBUG -ge 1 ] && echo "$DATE: == WGET PROCESSING ==" >> $LOG
[ $DEBUG -ge 1 ] && echo "$DATE: Run get.sh" >> $LOG
/conf/.cron/get.sh
RETVAL=$?
DAT=`date +"%F %T"`
[ $DEBUG -ge 1 ] && echo "$DATE: Return value: $RETVAL" >> $LOG
[ $DEBUG -ge 1 ] && echo "$DATE: = END runparts.sh at [$DAT] =" >> $LOG
